// subPackages/packageA/operatingRisk/landMortgage/landMortgage.ts
import { ZCC_TYPE } from "../../../../../typings/types/common";
import { Operation } from "../../../../../typings/types/operation";
import { getLandMortgageListAPI } from "../../api/operatingRisk";
import { ZCC } from "../../../../utils/BasePage";
import { formateDigit, zcc } from "../../../../utils/util";
import { OperationRiskVM } from "../details/commonDetail/commonDetailVM";

interface PageData {
  companyId: string,
  navTitle: string
}
new ZCC.RefreshListPage<any>({

  // 搜索框输入关键字
  searchChange(e: any) {
    console.log(this.data.query)
    this.onPullDownRefresh()
  },
  onClick(e: any) {
    const id = e.currentTarget.dataset.id
    console.log('id = ' + id);
    const navtitle = this.data.navTitle
    zcc.goOperationRiskDetail(id, OperationRiskVM.Scene.landMortgage, navtitle)
  },
  async getListDatas() {
    const that = this
    this.setRefreshing()
    try {
      const res = await getLandMortgageListAPI(this.data.companyId, this.data.page)
      if (res.content) {
        console.log(res.content)
        let tmp = this.processListDatas(res.content)
        that.updateDatasWithNewItems(tmp, res.content.total, res.content.hasNextPage)
      }
    } finally {
      that.endRefreshing()
    }
  },
  processListDatas(content: ZCC_TYPE.ListResult<Operation.LandMortgageList>) {

    const cells = content.list.flatMap((item: Operation.LandMortgageList) => {

      let tmp = [
        {
          title: item.usufructNo || '-',
          id: item.id,
          rows: [
            { title: '宗地地址：', val: item.address, ishorizontal: true },
            { title: '抵押土地用途：', val: item.mortgagePurpose, ishorizontal: true },
            { title: '抵押土地权属性质与使用权类型：', val: item.natureAndType, ishorizontal: true },
            { title: '抵押面积：', val: item.mortgageAcreage, style: 'color:#F04040', valsubfix: ' 公顷', ishorizontal: true },
            { title: '抵押金额：', val: formateDigit(item.mortgagePrice), style: 'color:#F04040', valsubfix: ' 万元', ishorizontal: true },
            { title: '评估金额：', val: formateDigit(item.assessmentPrice), style: 'color:#F04040', valsubfix: ' 万元', ishorizontal: true },
            { title: '土地抵押登记起始时间：', val: item.onBoardStartTime, ishorizontal: true },
            { title: '土地抵押结束时间：', val: item.onBoardEndTime, ishorizontal: true },
          ]
        }
      ]
      return tmp
    })
    return cells
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    companyId: '',
    navTitle: '土地抵押'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getListDatas()
  },

  onPullDownRefresh() {
    if (this.resetPageIfNeed()) {
      this.getListDatas()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getListDatas()
  },
})